package com.mxdx.edu.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mxdx.edu.common.ApiResult;
import com.mxdx.edu.common.CommonUtils;
import com.mxdx.edu.common.log.AppLogger;
import com.mxdx.edu.enums.ErrorEnums;
import com.mxdx.edu.model.RecommendStudent;
import com.mxdx.edu.service.ICacheService;
import com.mxdx.edu.service.IRecommendStudentService;
import com.mxdx.edu.service.ISerialService;
import com.mxdx.edu.util.StringUtils;

/**
 * 继续教育-推荐报名
 * 
 * @author WangYuan
 * @date 2018-02-12
 *
 */

@Controller
@RequestMapping("/continuingEducation")
public class RecommendEnrollController extends BaseController{

	@Resource
	private ICacheService cacheService;
	
	@Resource
	private ISerialService serialService;
	
	@Resource
	private IRecommendStudentService recommendStudentService;
	
	private AppLogger logger = new AppLogger(RecommendEnrollController.class);

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/auth/recommendEnroll")
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("h5/tuijianbaoming");
		String token = sessionUtil.getTokenSession(request);
		Map<String,Object> studentMap = (Map<String, Object>)redisUtil.get(token);
		String mood = CommonUtils.getStrValueFromMap(studentMap, "mood", "");
		String studentName = CommonUtils.getStrValueFromMap(studentMap, "studentName", "");
		String headPortrait = CommonUtils.getStrValueFromMap(studentMap, "headPortrait", "");
		mav.addObject("mood", mood);
		mav.addObject("studentName", studentName);
		mav.addObject("headPortrait", headPortrait);
		
		mav.addObject("levelList", cacheService.getDictList("level"));
		mav.addObject("gcf", cacheService.getDictList("graduate_certificate_form"));
		mav.addObject("educationDegreeList", cacheService.getDictList("education_degree"));
		
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value = "/auth/recommendEnroll/save")
	public ApiResult<Map<String, Object>> save(HttpServletRequest request,
			HttpServletResponse response) {
		logger.entry(request.getRemoteHost(), "save");
		ApiResult<Map<String, Object>> apiResult = new ApiResult<>();
		try{
			RecommendStudent recommendStudent = new RecommendStudent();
			recommendStudent.setStudentId(super.getStudentId(request));
			recommendStudent.setId(serialService.generateSerialNum("", 8));
			recommendStudent.setRefereeName(request.getParameter("name"));
			recommendStudent.setNation(request.getParameter("nation"));
			recommendStudent.setIdNumber(request.getParameter("idCard"));
			recommendStudent.setSex(Integer.parseInt(request.getParameter("sex")));
			recommendStudent.setWechatNumber(request.getParameter("webchat"));
			recommendStudent.setEducationDegree(Integer.parseInt(request.getParameter("educationDegree")));
			recommendStudent.setMailingAddress(request.getParameter("postalAddress"));
			recommendStudent.setContactTelephone(request.getParameter("mobilePhone"));
			recommendStudent.setApplySchool(request.getParameter("registerCollege"));
			recommendStudent.setApplyMajor(request.getParameter("major"));
			recommendStudent.setApplyLevel(Integer.parseInt(request.getParameter("level")));
			recommendStudent.setGraduateSchool(request.getParameter("graduationSchool"));
			recommendStudent.setGraduateMajor(request.getParameter("graduateMajor"));
			if(StringUtils.isNotBlank(request.getParameter("graduateCertificateForm"))){
				recommendStudent.setGraduateCertificateForm(Integer.parseInt(request.getParameter("graduateCertificateForm")));
			}
			recommendStudent.setGraduateCertificateNumber(request.getParameter("graduationCertificateId"));
			recommendStudent.setCreateDate(new Date());
			recommendStudent.setUpdateDate(new Date());
			if(!recommendStudentService.saveRecommendStudent(recommendStudent)){
				apiResult.setStatus(ErrorEnums.SAVE_ERROR.getCode());
				apiResult.setMessage(ErrorEnums.SAVE_ERROR.getDesc());
				logger.error(JSONObject.toJSONString(apiResult));
			}
			logger.info(JSONObject.toJSONString(apiResult));
		}catch(Exception e){
			e.printStackTrace();
			apiResult.setStatus(ErrorEnums.SYSTEM_ERROR.getCode());
			apiResult.setMessage(ErrorEnums.SYSTEM_ERROR.getDesc());
			logger.error(JSONObject.toJSONString(apiResult));
		}
		return apiResult;
	}

}
